<template>
    <div class="con">
        <img src="../assets/images/logo.png" alt="" class="logo" onclick="return false">
        <img src="../assets/images/admin_wood.png" alt="" onclick="return false"> 
        <img src="../assets/images/btn_1.png" alt="" class="title title1" @click.prevent="scan()">
        <router-link to="/cashTicket">
            <img src="../assets/images/btn_2.png" alt="" class="title title2" >
        </router-link>
        <router-link to="/ticketStatistics">
            <img src="../assets/images/btn_3.png" alt="" class="title title3">
        </router-link>
 
         <img src="../assets/images/btn_4.png" alt="" class="title title4" @click.prevent="scanGift()"> 
   
    </div>
</template>

<script>
export default {
    name: "admin",
    data() {
        return {
            msg: "Welcome to Your Vue.js App"
        };
    },
    beforeCreate() {
        document.getElementsByTagName("body")[0].className = "bg_MyPoints";
    },
    methods: {
        scan(e) {
            console.log(e)
            wx.scanQRCode({
                desc: "scanQRCode desc",
                needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                success: res => {
                    var result = res.resultStr;
                    var qs = require("qs");
                    this.axios({
                        method: "post",
                        url: `${this.siteUrl}/piaowu/api/check_order_online`,
                        data: qs.stringify({
                            member_id: $.cookie("osc_member_id"),
                            token: $.cookie("osc_member_token"),
                            staff_id: localStorage.getItem("staff_id"),
                            staff_token: localStorage.getItem("staff_token"),
                            code: result
                        })
                    })
                        .then(response => {
                            if (response.data.status == 1) {
                                this.$router.push({ name: "resultSuccess" });
                            } else {
                                alert(response.data.msg);
                                this.$router.push({ name: "resultError" });
                            }
                        })
                        .catch(function(error) {
                            console.log(error);
                        });
                },
                error: function(res) {
                    if (res.errMsg.indexOf("function_not_exist") > 0) {
                        alert("版本过低请升级");
                    }
                }
            });
        },
        scanGift() {
            wx.scanQRCode({
                desc: "scanQRCode desc",
                needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                success: res => {
                    var result = res.resultStr;
                    var qs=require("qs");
                    this.axios({
                        method: "post",
                        url: `${this.siteUrl}/piaowu/api/gift_exchange`,
                        data: qs.stringify({
                            member_id: $.cookie("osc_member_id"),
                            gift_member_id: $.cookie("osc_member_id"),
                            token: $.cookie("osc_member_token"),
                            staff_id: localStorage.getItem("staff_id"),
                            staff_token: localStorage.getItem("staff_token"),
                            gift_id: result
                        })
                    })
                        .then(response => {                     
                            if (response.data.status == 1) {
                                alert("兑换成功");
                            } else {
                                alert("请求失败：" + response.data.msg);
                            }
                        })
                        .catch(function(error) {
                            alert(error);
                        });
                },
                error: function(res) {
                    if (res.errMsg.indexOf("function_not_exist") > 0) {
                        alert("版本过低请升级");
                    }
                }
            });
        }
    },
    mounted: function() {
        var qs=require("qs");
        this.axios({
            method: "post",
            url: `${this.siteUrl}/piaowu/api/get_js_sign`,
            data: qs.stringify({             
                    url: location.host + location.pathname
                })
        })
            .then(response => {
                wx.config({
                    debug: false,
                    appId: response.data.data.appId,
                    timestamp: response.data.data.timestamp,
                    nonceStr: response.data.data.nonceStr,
                    signature: response.data.data.signature,
                    jsApiList: ["scanQRCode"]
                });
            })
            .catch(err => {
                console.log(err);
            });
    }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.con {
    text-align: center;
    position: relative;
}
.con img {
    width: 92%;
}
.con .logo {
    width: 34%;
    position: absolute;
    left: 50%;
    transform: translate(-50%, 30%);
}
.con .title {
    width: 40%;
    position: absolute;
    left: 50%;
    transform: translate(-50%, 0%);
}
.con .title1 {
    top: 26%;
}
.con .title2 {
    top: 41%;
}
.con .title3 {
    top: 56%;
}
.con .title4 {
    top: 71%;
}
</style>
